package com.bree.erp.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.NamedQueries;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@NamedQueries({
	@NamedQuery(name = Returns.QUERY_RETURNS_GROUPBY_WARESHOUSEPARTUNIT, query = "SELECT new com.bree.erp.entity.Returns(r.warehouse, r.partUnit, SUM(r.quantity)) FROM Returns r WHERE r.partUnit = :partUnit GROUP BY r.warehouse, r.partUnit HAVING SUM(r.quantity) > 0")
})
@Entity
public class Returns implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final String QUERY_RETURNS_GROUPBY_WARESHOUSEPARTUNIT = "groupByWarehousePartunit";

	public static final int COL_REMARKS_LEN = 255;
	public static final int COL_LOCATION_LEN = 50;
	public static final int COL_MOVEMENTTYPE_LEN = 15;

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	private Long id;
	
	@Transient
	private int itemNumber;

	@Temporal(TemporalType.DATE)
	private Date entryDate;

	@Column(length=COL_MOVEMENTTYPE_LEN)
	@Enumerated(EnumType.STRING)
	private MovementType movementType;

	@OneToOne
	private Project project;

	@OneToOne
	private Warehouse warehouse;

	@OneToOne
	private PartUnit partUnit;

	private double quantity;


	@Column(length=COL_LOCATION_LEN)
	private String location;


	@Column(length=COL_REMARKS_LEN)
	private String remarks;

	@OneToOne
	private User createdBy;
	@OneToOne
	private User updatedBy;

	@Temporal(TemporalType.TIMESTAMP)
	private Date created;
	@Temporal(TemporalType.TIMESTAMP)
	private Date updated;
	
	public Returns() {
		
	}

	public Returns(Warehouse warehouse, PartUnit partUnit, double quantity) {
		this.warehouse = warehouse;
		this.partUnit = partUnit;
		this.quantity = quantity;
	}
	
	@Override
	public boolean equals(Object object) {
		
		if(this == object) {
			return true;
		}
		
		if(object instanceof Returns) {
			Returns that = (Returns) object;
			return this.id != null && this.id.equals(that.id);
		}
		
		return false;
	}
	
	@Override
	public int hashCode() {
		return this.id == null ? 0 : this.id.hashCode();
	}
	
	public WarehousePartUnitId getWarehousePartUnitId() {
		return new WarehousePartUnitId(this.warehouse, this.partUnit);
	}
	
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the entryDate
	 */
	public Date getEntryDate() {
		return entryDate;
	}

	/**
	 * @param entryDate
	 *            the entryDate to set
	 */
	public void setEntryDate(Date entryDate) {
		this.entryDate = entryDate;
	}

	/**
	 * @return the movementType
	 */
	public MovementType getMovementType() {
		return movementType;
	}

	/**
	 * @param movementType
	 *            the movementType to set
	 */
	public void setMovementType(MovementType movementType) {
		this.movementType = movementType;
	}

	/**
	 * @return the project
	 */
	public Project getProject() {
		return project;
	}

	/**
	 * @param project
	 *            the project to set
	 */
	public void setProject(Project project) {
		this.project = project;
	}

	/**
	 * @return the warehouse
	 */
	public Warehouse getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse
	 *            the warehouse to set
	 */
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	/**
	 * @return the partUnit
	 */
	public PartUnit getPartUnit() {
		return partUnit;
	}

	/**
	 * @param partUnit
	 *            the partUnit to set
	 */
	public void setPartUnit(PartUnit partUnit) {
		this.partUnit = partUnit;
	}

	/**
	 * @return the quantity
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity
	 *            the quantity to set
	 */
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the location
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * @param location
	 *            the location to set
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * @return the remarks
	 */
	public String getRemarks() {
		return remarks;
	}

	/**
	 * @param remarks
	 *            the remarks to set
	 */
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	/**
	 * @return the createdBy
	 */
	public User getCreatedBy() {
		return createdBy;
	}

	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	public void setCreatedBy(User createdBy) {
		this.createdBy = createdBy;
	}

	/**
	 * @return the updatedBy
	 */
	public User getUpdatedBy() {
		return updatedBy;
	}

	/**
	 * @param updatedBy
	 *            the updatedBy to set
	 */
	public void setUpdatedBy(User updatedBy) {
		this.updatedBy = updatedBy;
	}

	/**
	 * @return the created
	 */
	public Date getCreated() {
		return created;
	}

	/**
	 * @param created
	 *            the created to set
	 */
	public void setCreated(Date created) {
		this.created = created;
	}

	/**
	 * @return the updated
	 */
	public Date getUpdated() {
		return updated;
	}

	/**
	 * @param updated
	 *            the updated to set
	 */
	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	/**
	 * @return the itemNumber
	 */
	public int getItemNumber() {
		return itemNumber;
	}

	/**
	 * @param itemNumber the itemNumber to set
	 */
	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}
}
